<?php
class ConfigurationController extends Zend_Controller_Action
{
	protected $_configData;
	protected $_acl;
	protected $_userGroup;
	
	public function init()
	{
		$auth = Zend_Auth::getInstance();
		$userInfo = $auth->getStorage()->read();
		$groupBase = new GroupBase();
		if ($userInfo) {
			$userGroups = $groupBase->getUserGroup($userInfo->name);
		}
		$this->_userGroup = $userGroups[0]['name'] ? $userGroups[0]['name'] : "Users";
		$this->_acl = new Acl_PPMIS();
		$this->view->acl = $this->_acl;
		$this->view->userGroup = $this->_userGroup;
	}
	
	public function preDispatch()
	{
		$auth = Zend_Auth::getInstance();
		if($auth->getIdentity()) {
			$this->view->auth = "success";
			$userInfo = $auth->getStorage()->read();
			$this->view->userInfo = $userInfo;
		} else {
			$this->_redirect('auth/login');
		}
		$Configuration = new Configuration();
		$configurationData = $Configuration->getConfigurationInfo();
		$this->_configData = $configurationData;
		$this->view->configuration = $configurationData;
	}
	
	public function indexAction()
	{		
		$form = new BasicConfigurationForm();
		$form->populate($this->_configData);
		if ($this->getRequest()->isPost()) {
			$formData = $this->_request->getPost();
			if ($form->isValid($formData)) {
				Zend_Debug::dump($formData);
			}
		}
		$this->view->form = $form;
	}
	
	public function gridAction()
	{
		$LayoutItem = new LayoutItems();
		$data = $LayoutItem->getAdapter()->fetchAll($LayoutItem->select()->order('position ASC'));
		$operation = $this->_getParam('operation');
		$position = $this->_getParam('position');
		 
		/*$move = $this->_getParam('move');
		$position = $this->_getParam('position');
		$field = $this->_getParam('field');
		if ($move) {
			if($move == 'left') {
				for ($i=0;$i<count($data);$i++) {
					if($position == $i) {
						$temp = $data[$position]['field'];
						$tempPosition = $data[$position+1]['position'];
						$data[$position]['field'] = $field;
						$data[$position]['position'] = $position;
						$data[($position+1)]['field'] = $temp;
						$data[($position+1)]['position'] = $tempPosition;
							
					}
				}
			
			} elseif ($move == 'right') {
				for ($i=0;$i<count($data);$i++) {
					if($position == $i) {
						$temp = $data[$position]['field'];
						$tempPosition = $data[$position-1]['position'];
						$data[$position]['field'] = $field;
						$data[$position]['position'] = $position;
						$data[($position-1)]['field'] = $temp;
						$data[($position-1)]['position'] = $tempPosition;	
					}
				}
			}
			//Zend_Debug::dump($data);
			$LayoutItem->delete('');
			foreach ($data as $d) {
				$LayoutItem->insert($d);
			}
		}*/
		$this->view->data = $data;
	}
}